package com.culturalCenter.dataCenter.service.impl.training;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.training.TrainingSiteMonitorResponse;
import com.culturalCenter.dataCenter.customEntity.training.CustomTrainingPeriods;
import com.culturalCenter.dataCenter.customEntity.training.CustomUserVO;
import com.culturalCenter.dataCenter.entity.site.SiteAlerts;
import com.culturalCenter.dataCenter.entity.training.TrainingMonitorPersonnel;
import com.culturalCenter.dataCenter.entity.training.TrainingSiteMonitorCount;
import com.culturalCenter.dataCenter.mapper.site.SiteAlertsMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingMonitorPersonnelMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingPeriodsMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingSiteMonitorCountMapper;
import com.culturalCenter.dataCenter.service.training.TrainingSiteMonitorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : weicai
 * @date : 11:23 2020/10/27
 */
@Service
public class TrainingSiteMonitorServiceImpl implements TrainingSiteMonitorService {

    @Resource
    private TrainingSiteMonitorCountMapper trainingSiteMonitorCountMapper;
    @Resource
    private TrainingMonitorPersonnelMapper trainingMonitorPersonnelMapper;
    @Resource
    private SiteAlertsMapper siteAlertsMapper;
    @Resource
    private TrainingPeriodsMapper trainingPeriodsMapper;

    @Override
    public TrainingSiteMonitorResponse getSiteMonitor(String trainingId, String periodsId, int pageNum, int pageSize) {
        TrainingSiteMonitorResponse trainingSiteMonitorResponse = new TrainingSiteMonitorResponse();
        // 期数列表
        List<CustomTrainingPeriods> trainingPeriodsList = trainingPeriodsMapper.queryAllByTrainingId(trainingId);
        trainingSiteMonitorResponse.setPeriodsList(trainingPeriodsList);
        if (CollectionUtil.isNotEmpty(trainingPeriodsList)) {
            if (StrUtil.isEmpty(periodsId)) {
                periodsId = trainingPeriodsList.get(0).getId();
            }
        }
        // 该期的人流曲线
        List<TrainingSiteMonitorCount> crowdList = trainingSiteMonitorCountMapper.queryCrowd(periodsId);
        if (CollectionUtils.isNotEmpty(crowdList)) {
            trainingSiteMonitorResponse.setCrowdList(crowdList);
        }
        // 出入人员监控记录列表（主要是有监控画面，所以用inner）
        PageHelper.startPage(pageNum, pageSize);
        List<CustomUserVO> attendanceList = trainingMonitorPersonnelMapper.queryPersonnel(periodsId, pageNum, pageSize);
        PageInfo<CustomUserVO> pageInfo = new PageInfo<>(attendanceList);
        trainingSiteMonitorResponse.setAttendanceList(pageInfo);
        // 监控视频与图片
        //        List<TrainingSiteMonitor> monitorPictureList = siteAlertsMapper.queryResource(trainingId, periodsId);
        //        trainingSiteMonitorResponse.setMonitorPictureList(monitorPictureList);
        return trainingSiteMonitorResponse;
    }

    @Override
    public PageInfo<SiteAlerts> getException(String periodsId, int pageNum, int pageSize) {
        List<SiteAlerts> exceptionList = siteAlertsMapper.queryException("", periodsId, pageNum, pageSize);
        return new PageInfo<>(exceptionList);
    }

    @Override
    public TrainingMonitorPersonnel getPersonMonitorPicture(String id) {
        return trainingMonitorPersonnelMapper.selectPicture(id);
    }

}
